package edu.xjdx.os.controller;

import edu.xjdx.os.domain.Customer;
import edu.xjdx.os.service.CustomerService;
import edu.xjdx.os.utils.IDGenerator;
import edu.xjdx.os.utils.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    CustomerService service;

    @Autowired
    JavaMailSender mailSender;

    @ResponseBody
    @RequestMapping("/login")
    public Integer login(String customerName, String customerPassword, HttpSession session){
        return service.login(customerName,customerPassword,session);
    }

    @ResponseBody
    @RequestMapping("/register")
    public Integer register(String customerName,String customerEmail,String customerPassword,String customerPhone,String customerAddress){
        return service.register(customerName,customerEmail,customerPhone,customerAddress,customerPassword);
    }
    @PostMapping("/add")
    public String addCustomer(Customer customer){
        if(service.addCustomer(customer)==0){
            return "redirect:/customer/list";
        }else if(service.addCustomer(customer)==1){
            return "back/customer_add";
        }
        return "back/customer_add";
    }

    @GetMapping("/add")
    public String addCustomer(){
        return "back/customer_add";
    }


    @RequestMapping("/list")
    public String listCustomer(Model model){
        List<Customer> customerList=service.listAll();
        model.addAttribute("customer",customerList);
        return "back/customer_list";
    }

    @PostMapping("/delete")
    @ResponseBody
    public Object listDelete(String id){
        Map<String,Object> map=new HashMap<>();
        if(service.remove(id)){
            map.put("ok",true);
        }else{
            map.put("ok",false);
        }

        return map;
    }


    @RequestMapping(value = "/modify",method = RequestMethod.POST)
    public String update(Customer customer){
        if(service.update(customer)==true){
            return "redirect:/customer/list";
        }else {
            return "redirect:/customer/list";
        }
    }

    @GetMapping("/homepage")
    public String homepage(){
//        Customer customer=new Customer();
//        customer=service.findById(customer1.getCustomerId());
//        model.addAttribute("customerAddress",customer.getCustomerAddress());
//        model.addAttribute("customerPhone",customer.getCustomerPhone());
//        model.addAttribute("customerEmail",customer.getCustomerEmail());
        return "fore/homepage";
    }

    @GetMapping("/update")
    public String Modify(){
        return "redirect:/fore/modify";
    }

    @PostMapping("/update")
//    String customerEmail, String customerPhone, String customerAddress, Session session
    public String Modify(Customer customer,HttpSession session){
        if(service.update(customer)==true){
            session.setAttribute("customerId",customer.getCustomerId());
            session.setAttribute("customerName",customer.getCustomerName());
            session.setAttribute("customerEmail",customer.getCustomerEmail());
            session.setAttribute("customerPhone",customer.getCustomerPhone());
            session.setAttribute("customerAddress",customer.getCustomerAddress());
            session.setAttribute("customerPassword",customer.getCustomerPassword());
            return "redirect:/fore/homepage";
        }

        return "/fore/modify";
    }

    @ResponseBody
    @PostMapping("change")
    public Integer change(String customerPassword,String newPassword,String Password,HttpSession session){
        String id1=(String) session.getAttribute("customerId");
        Customer customer = service.findById(id1);
        return service.change(customer,customerPassword,newPassword,Password);
    }

    @ResponseBody
    @RequestMapping("/findEmail")
    public Integer findEmail(String customerName,String customerEmail,HttpSession session){
        return service.findEmail(customerName,customerEmail,session);
    }

    @ResponseBody
    @RequestMapping("/check")
    public Integer check(String verification,HttpSession session){
        return service.check(verification,session);
    }

    @RequestMapping("/forget")
    @ResponseBody
    public Integer forget(String newPassword,HttpSession session,String confirm){
        Customer customer = (Customer)session.getAttribute("cus");
        return service.forget(newPassword,confirm,customer);
    }

//    @ResponseBody
//    @GetMapping("/sendmail")
//    public String sendMail(){
//        try {
//            MimeMessage mimeMessage = this.mailSender.createMimeMessage();
//            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
//            message.setFrom("865721755@qq.com");
//            message.setTo("1004623577@qq.com");
//            message.setSubject("测试邮件主题");
//            message.setText("测试邮件内容");
//            this.mailSender.send(mimeMessage);
//
//            return "sucesss";
//        } catch (Exception ex) {
//            ex.printStackTrace();
//            return "error";
//        }
//    }
}
